<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="value"></el-input>
      <el-button @click="removesearch">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="userList">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="editList(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="confirmdel(row.id)"
          >
            <template #reference>
              <el-button ref="but" size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page="params._page"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="openDialog">+ 新增</el-button>
    </div>
    <!-- 新增/编辑弹框 -->
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="30%"
      @close="closeDialog"
      close-on-click-modal
    >
      <el-form ref="form" label-width="80px" :model="addList" :rules="rules">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="addList.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="addList.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//7-27新增id转化未完成
import { obtainList, addList, editorList, deleteList, detailsList, searchList } from '@/api/list'
import { v4 as uuidv4 } from 'uuid'
export default {
  data() {
    return {
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: 0,
      userList: [],
      dialogVisible: false,
      addList: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [
          { required: true, message: '任务名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '任务名称要求1-50个字符', trigger: 'blur' }
        ],
        des: [
          { required: true, message: '任务描述不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '任务描述要求1-300个字符', trigger: 'blur' }
        ]
      },
      value: ''
    }
  },
  mounted() {
    this.renderList()
  },
  methods: {
    async renderList() {
      const res = await obtainList(this.params)
      // console.log(res, '1')
      this.userList = res.data

      this.total = +res.headers['x-total-count']
      // console.log(+this.total)
    },
    pageChange(page) {
      this.params._page = page
      this.renderList()
    },
    closeDialog() {
      this.dialogVisible = false
    },
    openDialog() {
      this.dialogVisible = true
    },
    confirm() {
      // 兜底校验
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.addList.id) {
            // 编辑接口
            await editorList(this.addList)
          } else {
            // 新增接口
            this.addList.id = uuidv4()
            await addList(this.addList)
          }
          this.dialogVisible = false
          this.$message.success(`${this.addList.id ? '编辑' : '新增'}数据成功`)
          //重置
          this.$refs.form.resetFields()
          this.addList = {
            name: '',
            des: '',
            id: ''
          }
          this.renderList()
        }
      })
    },

    async confirmdel(id) {
      await deleteList(id)
      this.renderList()
    },
    async editList(id) {
      this.dialogVisible = true
      const res = await detailsList(id)
      this.addList = res.data
    },
    async search() {
      const res = await searchList(this.value)
      // console.log(res)
      this.userList = res.data
      this.total = 1
    },
    removesearch() {
      this.value = ''
      this.renderList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
